package com.ooqiu.gaming.ui.feedback;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.ooqiu.gaming.R;
import com.ooqiu.gaming.api.FileManager;
import com.ooqiu.gaming.api.GamingApi;
import com.ooqiu.gaming.base.BaseActivity;
import com.ooqiu.gaming.components.retrofit.progress.ProgressSubscriber;
import com.ooqiu.gaming.view.ToastUtil;
import com.ooqiu.gaming.view.Topbar;
import com.ooqiu.gaming.view.WildRadioButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

@Route(path = FeedBackActivity.PATH)
public class FeedBackActivity extends BaseActivity {
    public static final String PATH = "/gn/feedback";
    @BindView(R.id.top_bar)
    Topbar mTopBar;
    @BindView(R.id.edit_feedback_content)
    EditText mEditFeedbackContent;
    @BindView(R.id.ra_feedback_bug)
    WildRadioButton mRaFeedbackBug;
    @BindView(R.id.ra_feedback_func)
    WildRadioButton mRaFeedbackFunc;
    @BindView(R.id.ra_feedback_content)
    WildRadioButton mRaFeedbackContent;
    @BindView(R.id.ra_feedback_advertise)
    WildRadioButton mRaFeedbackAdvertise;
    @BindView(R.id.ra_feedback_net)
    WildRadioButton mRaFeedbackNet;
    @BindView(R.id.ra_feedback_other)
    WildRadioButton mRaFeedbackOther;
    @BindView(R.id.lay_radio)
    GridLayout mLayRadio;
    @BindView(R.id.edit_constact)
    EditText mEditConstact;
    @BindView(R.id.ck_upload_log)
    CheckBox mCkUploadLog;
    @BindView(R.id.btn_submmit)
    Button mBtnSubmmit;
    @BindView(R.id.rv_imgs)
    RecyclerView mRvImgs;

    UploadImgAdapter mUploadImgAdapter;

    FileManager mFileManager;

    GamingApi mGamingApi;

    List<MultiItemEntity> mImgs = new ArrayList<>();

    @Override
    public int initContentView() {
        return R.layout.activity_feed_back;
    }

    @Override
    public void initInjector() {
        ButterKnife.bind(this);
        mFileManager = getApplicationComponent().getFileManager();
        mGamingApi = getApplicationComponent().getGamingApi();
    }

    @Override
    public void initUiAndListener() {


        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 5);
        mRvImgs.setLayoutManager(layoutManager);
        mRvImgs.setAdapter(mUploadImgAdapter = new UploadImgAdapter(this, mFileManager, mImgs, 5));

//        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRvImgs.getContext(), GridLayoutManager.HORIZONTAL);

//        Drawable drawable = new ColorDrawable(Color.GREEN);
//        drawable.setBounds(0, 0, 50, 50);
//        LogUtil.e("drawable width = " + drawable.getIntrinsicWidth());

//        dividerItemDecoration.setDrawable(drawable);

//        mRvImgs.addItemDecoration(dividerItemDecoration);
//        mRvImgs.addItemDecoration(new DividerItemDecoration(mRvImgs.getContext(), GridLayoutManager.VERTICAL));

    }


    @OnClick(R.id.btn_submmit)
    public void feedback() {
        WildRadioButton rb = null;
        for (int i = 0; i < mLayRadio.getChildCount(); i++) {
            WildRadioButton tempRb = (WildRadioButton) mLayRadio.getChildAt(i);
            if (tempRb.isChecked()) {
                rb = tempRb;
                break;
            }


        }

        if (rb == null) {
            ToastUtil.showWarning("请选择反馈类型");
            return;
        }
        if (TextUtils.isEmpty(mEditFeedbackContent.getText())) {
            ToastUtil.showWarning("请填写反馈内容");
            return;
        }
        if (TextUtils.isEmpty(mEditConstact.getText())) {
            ToastUtil.showWarning("请填写联系方式");
            return;
        }
        mGamingApi.feedback(rb.getCurCheck().getText().toString(), mEditFeedbackContent.getText().toString(), mEditConstact.getText().toString(), mUploadImgAdapter.getImgs())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<String>(mContext) {
                    @Override
                    public void onNext(String s) {
                        ToastUtil.showSuccess("提交成功");
                        finish();
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }


    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @Override
    protected Toolbar toolbarIdRes() {
        return mTopBar;
    }

    @Override
    protected View statusLayoutId() {
        return null;
    }
}
